home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Apple Script / OSAX / Return Record OSAX / GetFontInfo2.c < prev    next >
C/C++ Source or Header  |  1993-05-07  |  3KB  |  123 lines

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    GetFontInfo2.c
  4. //    Written by Donald Olson
  5. //    Copyright ®1993 Apple Computer Inc.
  6. //    All rights reserved.
  7. //
  8. //    This is a simple Scripting Addition that returns the FontInfo of the
  9. //    specified font and size. If the size parameter is not specified, default
  10. //    size of 12 is used. Written for a WWDC presentation by Donald Olson
  11. //    and Donn Denman.
  12. //
  13. //    Syntax: fontinfo <font name> [size <short>]
  14. //    Returns: record {leading:short;ascent:short;descent:short; max width: short}
  15. //
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17.  
  18. // Our header
  19. Boolean GetFontNumber( Str255 fontName, short *fontNum );
  20.  
  21. // Our includes
  22. #include <string.h>
  23. #include <Quickdraw.h>
  24. #include <Fonts.h>
  25. #include <Errors.h>
  26. #include <AppleEvents.h>
  27.  
  28. /*struct FontInfo {
  29.     short ascent;
  30.     short descent;
  31.     short widMax;
  32.     short leading;
  33. };
  34. */
  35.  
  36. // These are the constants defined for the properties we defined to
  37. // get AppleScript to return a record with our font info in it.
  38.  
  39. #define kLeading        'lead'
  40. #define kAscent        'sent'
  41. #define kDescent        'desc'
  42. #define kMaxWidth    'mxwd'
  43.  
  44. //AEVTOLIEfinf
  45.  
  46. #define keyFONTSIZE    'FSIZ'
  47.  
  48. pascal OSErr main(AppleEvent *theEvent, 
  49.                 AppleEvent *theReply,
  50.                  long theRefCon)
  51. {        
  52.     OSErr        theErr = noErr;
  53.     GrafPort        fontPort;
  54.     GrafPtr        savedPort, fontPortPtr = &fontPort;
  55.     Str255        fontName;
  56.     long            actualSize;
  57.     short        fontSize, fontNum;
  58.     DescType    typeCode;
  59.     FontInfo        fInfo;
  60.     AERecord    ourRecord = {typeNull, nil};
  61.     
  62.     theErr = AEGetParamPtr(theEvent, keyDirectObject, 
  63.                 typeChar, &typeCode, (Ptr)&fontName, 
  64.                 sizeof(fontName), &actualSize);
  65.     
  66.     if(theErr != noErr)
  67.         return theErr;
  68.     
  69.     fontName[actualSize] = '\0';        // c string please
  70.     c2pstr((char*)fontName);        // and now pascal
  71.     
  72.     theErr = AEGetParamPtr(theEvent, keyFONTSIZE, 
  73.                 typeShortInteger, &typeCode, (Ptr)&fontSize, 
  74.                 sizeof(fontSize), &actualSize);
  75.     
  76.     if(theErr != noErr)
  77.         fontSize = 12;
  78.     
  79.     if(!GetFontNumber(fontName, &fontNum))
  80.         return fontDecError;
  81.                 
  82.     GetPort(&savedPort);
  83.     OpenPort(&fontPortPtr);
  84.     SetPort(&fontPortPtr);
  85.     
  86.     TextFont(fontNum);
  87.     TextSize(fontSize);
  88.     
  89.     GetFontInfo(&fInfo);
  90.     
  91.     SetPort(&savedPort);
  92.         
  93.     theErr = AECreateList(nil, 0, true, &ourRecord);
  94.     if(theErr != noErr) return theErr;
  95.     theErr = AEPutKeyPtr(&ourRecord, kLeading, typeShortInteger, (Ptr)&fInfo.leading, 
  96.                         sizeof(fInfo.leading));
  97.     theErr = AEPutKeyPtr(&ourRecord, kAscent, typeShortInteger, (Ptr)&fInfo.ascent, 
  98.                         sizeof(fInfo.ascent));
  99.     theErr = AEPutKeyPtr(&ourRecord, kDescent, typeShortInteger, (Ptr)&fInfo.descent, 
  100.                         sizeof(fInfo.descent));
  101.     theErr = AEPutKeyPtr(&ourRecord, kMaxWidth, typeShortInteger, (Ptr)&fInfo.widMax, 
  102.                         sizeof(fInfo.widMax));
  103.                         
  104.     theErr = AEPutParamDesc(theReply, keyDirectObject, &ourRecord);
  105.     
  106.     return theErr;
  107. }
  108.  
  109. Boolean GetFontNumber( Str255 fontName, short *fontNum )
  110. {
  111.     Str255     systemFontName;
  112.  
  113.     GetFNum( fontName, fontNum );
  114.     if ( *fontNum == 0) {
  115.         /* Either we didn't find the font, or we were looking for the system
  116.           * font. */
  117.         GetFontName( 0, systemFontName );
  118.         return EqualString( fontName, systemFontName, FALSE, FALSE );
  119.     }
  120.     else
  121.         return TRUE;
  122. }
  123.